package com.hbnu.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.mapper.AwardtypeMapper;
import com.hbnu.system.model.entity.Award_Type;
import com.hbnu.system.model.entity.Award_Type2;
import com.hbnu.system.model.vo.ChildrenNameId;
import com.hbnu.system.model.vo.NameId;
import com.hbnu.system.service.IAwardType2Service;
import com.hbnu.system.service.IAwardTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AwardTypeServiceImpl extends ServiceImpl<AwardtypeMapper, Award_Type> implements IAwardTypeService {
    @Autowired
    private IAwardType2Service iAwardType2Service;

    @Override
    public List<ChildrenNameId> getCascadingData() {
        List<ChildrenNameId> reseults = new ArrayList<>();
        List<NameId> midlist = new ArrayList<>();

        HashMap<String,Integer> type_map_1 = new HashMap<>(); // 获奖主大类所有map
        HashMap<String,Integer> type_map_2 = new HashMap<>(); // 获奖第二类所有map
//        HashMap<String,Integer> type_map_3 = new HashMap<>(); // 获奖second所有map

        List<Award_Type2> awardType2Lists = iAwardType2Service.getAwardType2Lists();

        // 先来做中间的midlist
        List<Award_Type> awardTypeLists = baseMapper.selectList(Wrappers.lambdaQuery(Award_Type.class));

        for (Award_Type award_type: awardTypeLists) {
            type_map_2.put(award_type.getAwardType2(),award_type.getType2ID());
        }
        for (Award_Type award_type: awardTypeLists) {
            type_map_1.put(award_type.getAwardType1(),award_type.getType1ID());
        }
//        System.out.println(type_map_1);
//        System.out.println(type_map_2);
//        List<> nameIds = new ArrayList(NameId);

        for (Map.Entry<String,Integer> entry : type_map_2.entrySet()) {
            if(entry.getValue() < 10){ //对应数据库中上半部分的数据 创建子类
                ChildrenNameId childrenNameId = new ChildrenNameId(entry.getKey(), entry.getValue());
                for (Award_Type2 award_type2 : awardType2Lists) {
                    childrenNameId.getChildren().add(new NameId(award_type2.getPaperSecondType(),award_type2.getPaperSecondID()));
                }
                midlist.add(childrenNameId);
            }else { //对应数据库中下半部分的数据 创建父类
                NameId nameId = new NameId(entry.getKey(), entry.getValue());
                midlist.add(nameId);
            }
        }

//        System.out.println(midlist);
        for (Map.Entry<String,Integer> entry: type_map_1.entrySet()) {
            ChildrenNameId childrenNameId = new ChildrenNameId(entry.getKey(), entry.getValue());
            for (NameId nameId : midlist) {
                if(childrenNameId.getValue() < 6 && nameId.getValue() < 10){
                    childrenNameId.getChildren().add(nameId);
                    if(!reseults.contains(childrenNameId))
                        reseults.add(childrenNameId);
                }else if(childrenNameId.getValue() >= 6 && nameId.getValue() > 10){
                    childrenNameId.getChildren().add(nameId);
                    if(!reseults.contains(childrenNameId))
                        reseults.add(childrenNameId);
                }
            }
        }

        return reseults;
    }
}
